package com.accounting.juanjuan.config;

import com.accounting.juanjuan.constant.PathConstant;
import com.accounting.juanjuan.interceptor.PathInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域配置
 *
 * @Author xxj
 */
@Configuration
public class CorsConfiguration implements WebMvcConfigurer {
    private final PathInterceptor pathInterceptor;

    public CorsConfiguration(PathInterceptor pathInterceptor) {
        this.pathInterceptor = pathInterceptor;
    }

    /**
     * 跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(PathConstant.ALLOWED_ORIGINS_PATH)
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(1800)
                .exposedHeaders(HttpHeaders.ACCEPT);
    }

    /**
     * 拦截器配置
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(pathInterceptor)
                .addPathPatterns("/**");
    }
}
